#include <vector>
#include <string>
#include <sstream>
#include <iostream>
#include "single_include/json.hpp"

std::vector<std::string> sepstr(const std::string &sStr, const std::string &delimiter)
{
    std::vector<std::string> vList;
    std::string::size_type pos = 0;
    std::string::size_type next = 0;
    while ((next = sStr.find(delimiter, pos)) != std::string::npos)
    {
        std::string token = sStr.substr(pos, next - pos);
        vList.emplace_back(token);
        pos = next + delimiter.length();
    }
    // 最后再把结尾追加到vector
    vList.emplace_back(sStr.substr(pos));
    return vList;
}

nlohmann::json getValue2(const nlohmann::json &jsonData, const std::vector<std::string> &keys)
{
    nlohmann::json value = jsonData;
    for (const std::string &key : keys)
    {
        std::vector<std::string> tokens = sepstr(key, ":");
        std::string firstKey = tokens.size() > 0 ? tokens[0] : "";
        std::string secondKey = tokens.size() > 1 ? tokens[1] : "";
        std::cout << "firstKey: " << firstKey << ", secondKey: " << secondKey << std::endl;
        if (secondKey == "parse")
        {
            try
            {
                std::string jsonString = value.operator[](firstKey);
                value = nlohmann::json::parse(jsonString);
            }
            catch (const std::exception &e)
            {
                std::cerr << "json parse error: " << e.what() << std::endl;
                return nlohmann::json();
            }
        }
        else if (value.is_null())
        {
            return value;
        }
        else if (value.is_object())
        {
            value = value[firstKey];
        }
        else if (value.is_array())
        {
            value = value[std::stoi(firstKey)];
        }
        else if (value.is_string() || value.is_boolean() || value.is_number())
        {
            return value;
        }
        else
        {
            std::cerr << "Invalid JSON structure" << std::endl;
            return nlohmann::json();
        }
    }

    return value;
}

int main()
{
    std::string jsonString = R"({
            "lastLogoutTime": -1,
            "serviceBranchId": "",
            "serviceBranch": "重庆分公司",
            "liveBodyRejReason": "",
            "authZStatus": 1,
            "userExtendInfo": "{\"rejectedReason\":\"\",\"step\":0,\"updateTime\":0,\"actionLog\":[],\"resultStr\":\"{\\\"area\\\":\\\"河北省\\/遵化市\\/东新庄镇前稻地村三小区\\\",\\\"address\\\":\\\"河北省遵化市东新庄镇前稻地村三小区208号\\\",\\\"postcode\\\":\\\"8896\\\",\\\"workunit\\\":\\\"zm\\\",\\\"income\\\":\\\"6666\\\",\\\"depart\\\":\\\"重庆分公司\\\",\\\"sameAddress\\\":true,\\\"selfControl\\\":true,\\\"selfBenefit\\\":false,\\\"integrityGood\\\":false,\\\"chineseTax\\\":true,\\\"departId\\\":\\\"1901\\\",\\\"educationKey\\\":10,\\\"education\\\":\\\"中专\\\",\\\"jobKey\\\":15,\\\"job\\\":\\\"普通职工\\\",\\\"jobPositionKey\\\":24,\\\"jobPosition\\\":\\\"信息运输、软件和信息技术服务人员\\\"}\",\"validDay\":-1,\"finishTime\":1682410714,\"status\":1,\"uid\":\"\"}",
            "authStep": 1,
            "openInfo": "{\"vOpenInfo\":[{\"sId\":\"\",\"sUionID\":\"\",\"sOpenID\":\"\",\"plat\":1}],\"vOpenInfo2\":\"vOpenInfo22\"}",
            "lastLoginTime": 1712806332,
            "mobileEnc": "",
            "headUrl": "https://dev.shgsec.com:16034/group0/M00/00/26/CjygMWOismiAYLjCAAAhBnCdnE40505137",
            "liveBodyCAInfo": "{\"rejectedReason\":\"\",\"step\":0,\"updateTime\":0,\"actionLog\":[],\"resultStr\":\"{\\\"sPortrait\\\":\\\"https:\\\\\\/\\\\\\/dev.shgsec.com:16034\\\\\\/group0\\\\\\/M00\\\\\\/00\\\\\\/2A\\\\\\/CjygMWQ3mz-AUu9OAAH4k109dyo1035852\\\",\\\"sFaceCompare\\\":\\\"97\\\"}\",\"validDay\":-1,\"finishTime\":1682410714,\"status\":1,\"uid\":\"sg_2bf2eaef58d9\"}",
            "regMN": "sdk",
            "regTime": 1657503963,
            "regMO": "Pixel 3",
            "reskTestCAInfo": "{\"rejectedReason\":\"\",\"step\":0,\"updateTime\":0,\"actionLog\":[],\"resultStr\":\"{\\\"score\\\":62,\\\"riskLevel\\\":\\\"积极型\\\",\\\"investDuring\\\":\\\"无特别要求\\\",\\\"investKinds\\\":\\\"股票、混合型基金、偏股型基金、股票型基金等权益类投资品种\\\",\\\"protocolUrl\\\":\\\"https:\\/\\/dev.shgsec.com:16034\\/group0\\/M00\\/00\\/2A\\/CjygMWQ-PK6Af7DVAAJNvVwWl_c554.pdf\\\",\\\"quesPdfUrl\\\":\\\"https:\\/\\/dev.shgsec.com:16034\\/group0\\/M00\\/00\\/2A\\/CjygMWQ-PK6AWAEhAAPD7_1HuJs848.pdf\\\"}\",\"validDay\":365,\"finishTime\":1681800346,\"status\":-3,\"uid\":\"\"}",
            "ch": "1234",
            "userAddressInfo": "",
            "sex": 0,
            "nickname": "188****1002",
            "sign": "6666666666",
            "status": 1,
            "updateTime": 1712806626,
            "password": "9f94ab1e5052958404c04f657b0b1452",
            "mobilePrx": "+86",
            "stopTime": -1,
            "uid": "sg_2bf2eaef58d9",
            "identNum": "",
            "deleteTime": -1,
            "clientId": "1002000008",
            "mobile": "18800001002",
            "fundsIds": "81002000008",
            "identCAInfo": "{\"rejectedReason\":\"\",\"step\":0,\"updateTime\":110,\"actionLog\":[],\"resultStr\":\"{\\\"address\\\":\\\"河北省遵化市东新庄镇前稻地村三小区208号\\\",\\\"birth\\\":\\\"1999\\/4\\/12\\\",\\\"id\\\":\\\"fedeefcea04a5b5f6de1a7c4a4df18b8ae6bc48313610b5fe0da679c169231be\\\",\\\"name\\\":\\\"李佳兴\\\",\\\"nation\\\":\\\"汉\\\",\\\"sex\\\":\\\"男\\\",\\\"frontSUrl\\\":\\\"https:\\/\\/dev.shgsec.com:16034\\/group0\\/M00\\/00\\/2D\\/CjygMWRGOVOALfzAAAKgnl_jC8M45.jpeg\\\",\\\"sPortrait\\\":\\\"https:\\/\\/dev.shgsec.com:16034\\/group0\\/M00\\/00\\/2D\\/CjygMWRGOVOAKVufAADK4937SLY44.jpeg\\\",\\\"authority\\\":\\\"遵化市公安局\\\",\\\"card_type\\\":\\\"1\\\",\\\"valid_date\\\":\\\"2019.02.20-2029.02.20\\\",\\\"backSUrl\\\":\\\"https:\\/\\/dev.shgsec.com:16034\\/group0\\/M00\\/00\\/2D\\/CjygMWRGOViAQvY9AAMKoozQDfE58.jpeg\\\",\\\"ID\\\":\\\"fedeefcea04a5b5f6de1a7c4a4df18b8ae6bc48313610b5fe0da679c169231be\\\"}\",\"validDay\":-1,\"finishTime\":1682410714,\"status\":1,\"uid\":\"\"}",
            "realName": "李佳兴",
            "test11":"{\"rejectedReason\":\"jinitaimei\",\"resultStr\":\"{\\\"birth\\\":\\\"1999\\/4\\/12\\\",\\\"ID\\\":\\\"fedeefcea04a5b5f6de1a7c4a4df18b8ae6bc48313610b5fe0da679c169231be\\\",\\\"frontSUrl\\\":\\\"https:\\/\\/dev.shgsec.com:16034\\/group0\\/M00\\/00\\/2D\\/CjygMWRGOVOALfzAAAKgnl_jC8M45.jpeg\\\"}\",\"validDay\":-1}"
        })";

    {
        std::cout << "----------nlohmann::json----------" << std::endl;
        std::string jsonString = "{\"rejectedReason\":\"jinitaimei\",\"resultStr\":\"{\\\"birth\\\":\\\"1999\\/4\\/12\\\",\\\"ID\\\":\\\"fedeefcea04a5b5f6de1a7c4a4df18b8ae6bc48313610b5fe0da679c169231be\\\",\\\"frontSUrl\\\":\\\"https:\\/\\/dev.shgsec.com:16034\\/group0\\/M00\\/00\\/2D\\/CjygMWRGOVOALfzAAAKgnl_jC8M45.jpeg\\\"}\",\"validDay\":-1}";
        nlohmann::json jsonData = nlohmann::json::parse(jsonString);
        std::cout << "jsonData: " << jsonData << std::endl;
        std::string jsonString2 = jsonData.operator[]("resultStr");
        nlohmann::json jsonData2 = nlohmann::json::parse(jsonString2);
        std::cout << "jsonData2:" << jsonData2 << std::endl;
        std::string jsonString3 = jsonData2.operator[]("birth");
        std::cout << "birth: " << jsonString3 << std::endl;
    }

    {
        std::cout << "----------nlohmann::json----------" << std::endl;
        nlohmann::json jsonData = nlohmann::json::parse(jsonString);
        std::vector<std::string> keys = sepstr("test11:parse.resultStr:parse.frontSUrl|string", "|");
        std::cout << "keys[0]: " << keys[0] << ", keys[1]: " << keys[1] << std::endl;
        std::vector<std::string> keys2 = sepstr(keys[0], ".");
        nlohmann::json resultValue = getValue2(jsonData, keys2);
        std::cout << "resultValue: " << resultValue << std::endl;
    }
}
